<?php
namespace Home\Controller;

class ShopController extends HomeController
{

    public function shop()
    {
        $pid = I('get.id');
        //dump($pid);
        $category = D('Category');
        $sname    = D('Sname');
        $sfres    = $category->where(array('id' => $pid))->find();
        $res      = $category->where(array('pid' => $pid))->select();
        $count    = count($res);
        $b        = array();
        foreach ($res as &$v) {
            $id   = $v['id'];
            $sres = $sname->field('shop_sname.* , shop_category.
            pid,shop_kname.kname,shop_val.vname,shop_details.price,shop_details.name,shop_img.file')->join('shop_category ON shop_category.
            id =shop_sname.cid')->join('shop_kname ON shop_kname.
            gid =shop_sname.id')->join('shop_val ON shop_val.
            kid =shop_kname.id')->join('shop_details ON shop_details.
            gid =shop_sname.id')->join('shop_img ON shop_img.
            did =shop_details.id')->where((array('shop_category.pid' => $id)))->select();
            $fcount = count($sres);
            //dump($sres);
            foreach ($sres as $v5) {
                $b[] = $v5;
            }
            //dump($cid);
            //dump($cres);
            $cres = $category->where((array('pid' => $id)))->select();
            //dump($cres);
            foreach ($cres as $vv) {
                $title         = $vv['title'];
                $v['title1'][] = $title;
                //dump($cres);
            }
            $v['count'] = $fcount;
            $title1[]   = $v['title1'];
            $title2[]   = $v['title1'][0];
        }
        //dump($sarr);
        foreach ($title1 as $vvv) {
            foreach ($vvv as $vvvv) {
                $a[] = $vvvv;
            }

        }
        foreach ($b as $v6) {
            $c[] = $v6['vname'];
            $d[] = $v6['kname'];
        }
        //dump($c);
        $res1 = array_unique($a);
        $sarr = array_unique($c);
        $karr = array_unique($d);
        $arr  = array_filter($b, function ($xx) {
            global $tmp;
            if ($xx['id'] == $tmp) {
                return false;
            } else {
                //dump($tmp);
                $tmp = $xx['id'];
                return true;
            }
        });
        //dump($arr);
        //M('Deatils')->field('shop_deatils.* ,')where()-select();
        $this->assign('sfres', $sfres);
        $this->assign('res', $res);
        $this->assign('res1', $res1);
        $this->assign('res2', $sarr);
        $this->assign('res3', $karr[0]);
        $this->assign('res4', $arr);
        $this->display();
    }
    public function lamp()
    {
        $this->display();
    }
    public function details()
    {
        $id = I('get.id');
        //dump($id);
        $sname   = D('Sname');
        $res     = $sname->field('shop_sname.*,shop_details.price')->join('shop_details ON shop_sname.id = shop_details.gid')->where(array('shop_sname.id' => $id))->select();
        $details = D('Details');
        $dres    = $details->where(array('gid' => $id))->find();
        $img     = D('Img');
        $ires    = $img->where(array('did' => $dres['id']))->select();
        $kname   = D('Kname');
        $kres    = $kname->where(array('gid' => $id))->select();
        $vname   = D('Val');
        $vres    = $vname->where(array('kid' => $kres[0]['id']))->select();
        //dump($dres);
        $this->assign('res', $res);
        $this->assign('dres', $dres);
        $this->assign('ires', $ires);
        $this->assign('kres', $kres);
        $this->assign('vres', $vres);
        $this->display();
    }
    public function dindan()
    {
        $id    = I('post.did');
        $count = I('post.count');
        $name  = I('post.user');
        //dump($name);
        $user = M('User')->field('id')->where(array('user' => $name))->select()[0];
        //dump($id);die;
        $detalis = M('details')->where(array('id' => $id))->find();
        // dump($detalis);
        $_POST['pic']   = $detalis['pic'];
        $_POST['price'] = $detalis['price'];
        $_POST['vid']   = $detalis['vid'];
        $_POST['vname'] = $detalis['name'];
        $_POST['sid']   = $detalis['gid'];
        $_POST['uid']   = $user['id'];
        $shopp          = D('Shopp');
        $auto           = $shopp->create();
        //dump($shopp);die;
        if ($auto) {
            //dump(I('post.'));
            // dump($auto);
            $shopp->add();
            $html             = $this->fetch('success');
            $return['status'] = 1;
            $return['info']   = 'success';
            $return['data']   = $html;
        } else {
            $html             = $this->fetch('error');
            $return['status'] = 0;
            $return['info']   = 'error';
            $return['data']   = $html;
        }
        $this->ajaxReturn($return);
    }
    public function cart()
    {
        $uid     = I('get.uid');
        $shopp   = D('Shopp');
        $res     = $shopp->field('shop_shopp.*,shop_sname.title')->join('shop_sname ON shop_sname.id = shop_shopp.sid')->where(array('shop_shopp.status' => 0, 'shop_shopp.uid' => $uid))->select();
        $sycount = array();
        $syprice = array();
        foreach ($res as $v) {
            $sycount[] = $v['count'];
            $syprice[] = ($v['price']) * ($v['count']);
        }
        $acount = array_sum($sycount);
        $aprice = array_sum($syprice);
        // dump($acount);
        // dump($arlcount);
        // dump($arlprice);
        foreach ($res as $k => &$tv) {
            $sycount[]      = $tv['count'];
            $tv['allcount'] = $acount;
            $tv['allprice'] = $aprice;
            $tv['k']        = $k;
            $tv['alprice']  = $syprice[$k];
        }
        //dump($res);
        // dump($allcount);
        // dump($arlcount);
        $this->assign('res', $res);
        //dump($res);
        $this->display();
    }
    public function shopdel()
    {
        $id    = I('post.id');
        $shopp = D('Shopp');
        $del   = $shopp->where(array('id' => $id))->delete();
        if ($del) {
            $return['status'] = 1;
            $return['info']   = 'success';
        } else {
            $return['status'] = 0;
            $return['info']   = 'error';
        }
        $this->ajaxReturn($return);
    }
    public function shopupd()
    {
        $id             = I('post.id');
        $_POST['count'] = I('post.count');
        // dump($id);
        // dump($count);
        $shopp = D('Shopp');
        $auto  = $shopp->create();
        if ($auto) {
            $shopp->field('count')->where(array('id' => $id))->save();
        }
        $res     = $shopp->field('shop_shopp.*,shop_sname.title')->join('shop_sname ON shop_sname.id = shop_shopp.sid')->where(array('shop_shopp.status' => 0))->select();
        $sycount = array();
        $syprice = array();
        foreach ($res as $v) {
            $sycount[] = $v['count'];
            $syprice[] = ($v['price']) * ($v['count']);
        }
        $acount = array_sum($sycount);
        $aprice = array_sum($syprice);
        // dump($allprice);
        // dump($arlcount);
        // dump($arlprice);
        foreach ($res as $k => &$tv) {
            $sycount[]      = $tv['count'];
            $tv['allcount'] = $acount;
            $tv['allprice'] = $aprice;
            $tv['k']        = $k;
            $tv['alprice']  = $syprice[$k];
            $this->assign('res', $res);
        }
        if ($auto) {
            $return['status'] = 1;
            $return['info']   = 'success';
        } else {
            $return['status'] = 0;
            $return['info']   = 'error';
        }
        $this->ajaxReturn($return);
    }
    public function order()
    {
        $uid = I('get.uid');
        if (IS_POST) {
            $uid   = I('post.uid');
            $address=I('post.address');
            $tall = I('post.tall');
            $name=I('post.name');
            $shopp = D('Shopp')->where(array('status' => 0, 'uid' => $uid))->select();
            $oid  =array();
            $pic   = array();
            $vid   = array();
            $vname   = array();
            $sid   = array();
            $price =array();
            $time=time();
            $count=array();
            //dump(time());die;
            foreach ($shopp as $value) {
                $pic[] = $value['pic'];
                $vid[] = $value['vid'];
                $vname[] = $value['vname'];
                $sid[]   = $value['sid'];
                $price[]   = $value['price'];
                $oid[]=$value['id'];
                $count[]=$value['count'];
            }
            $acount = count($shopp);
            $order=D('order');
            //dump($count);
            if($count){
              for($i=0;$i<$acount;$i++){
                  echo $order->add(array('price'=>$price[($i)],'vid'=>$vid[($i)],'sid'=>$sid[($i)],'vname'=>$vname[($i)],'uid'=>$uid,'pic'=>$pic[($i)],'oid'=>$oid[($i)],'count'=>$count[($i)],'address'=>$address,'name'=>$name,'tall'=>$tall,'status'=>0,'ctime'=>$time));
              } 
              if($order){

                 D('Shopp')->where(array('uid'=>$uid))->setField('status',1);
                 $this->redirect('Shop/invoicing?uid='.$uid);
              }
            }else{
                $this->error('下单失败',U('Shop/order'));
            }
        } else {
            $this->assign('uid', $uid);
            $this->display();
        }
    }
    public function invoicing(){
        $uid=I('get.uid');
        $res=D('Order')->where(array('status'=>0,'uid'=>$uid))->find();
        //dump($res);
        $this->assign('res',$res);
        $this->display();
    }
}
